package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ShopInfoAdapter;
import com.luck.easyfoodbatch.entity.ShopInfo;
import com.luck.easyfoodbatch.mvp.model.ShoperModel;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.decoration.DividerGridItemLineDecoration;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopInfoT1Activity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    private ShopInfoAdapter adapter;
    private List<ShopInfo> list = new ArrayList<>();
    private ShopInfo shopInfo;
    private int seller_type = 1;//1公司2个体3生产厂家4批发商
    private ShoperModel.DataBean data;
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_info, "卖家认证1/3", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            // 编辑模式
            data = (ShoperModel.DataBean) getIntent().getSerializableExtra("data");
            seller_type = data.getSeller_type();
        }
        for (int i = 0; i < 4; i++) {
            shopInfo = new ShopInfo();
            switch (i) {
                case 0:
                    shopInfo.setChecked(true);
                    shopInfo.setName("公司");
                    shopInfo.setId(1 + "");
                    break;
                case 1:
                    shopInfo.setChecked(false);
                    shopInfo.setName("个人");
                    shopInfo.setId(2 + "");
                    break;
                case 2:
                    shopInfo.setChecked(false);
                    shopInfo.setName("生产厂家");
                    shopInfo.setId(3 + "");
                    break;
                case 3:
                    shopInfo.setChecked(false);
                    shopInfo.setName("批发商");
                    shopInfo.setId(4 + "");
                    break;
            }
            list.add(shopInfo);
        }

        if (seller_type != 0) {
            for (ShopInfo shopInfo : list) {
                shopInfo.setChecked(false);
            }
            switch (seller_type) {
                case 1:
                    list.get(0).setChecked(true);
                    break;
                case 2:
                    list.get(1).setChecked(true);
                    break;
                case 3:
                    list.get(2).setChecked(true);
                    break;
                case 4:
                    list.get(3).setChecked(true);
                    break;
            }
        }
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerGridItemLineDecoration(App.getInstance().getContext().getResources().getColor(R.color.white)));
        recyclerView.setLayoutManager(manager);
        adapter = new ShopInfoAdapter(mContext);
        recyclerView.setAdapter(adapter);
        adapter.setList(list);
        adapter.setOnItemClickListener(new ShopInfoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                for (ShopInfo shopInfo : list) {
                    shopInfo.setChecked(false);
                }
                ShopInfo shopInfo = list.get(position);
                if (shopInfo.isChecked()) {
                    shopInfo.setChecked(false);
                } else {
                    shopInfo.setChecked(true);
                    seller_type = Integer.parseInt(shopInfo.getId());
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.btn_exchange)
    public void onClick() {
        if (type == 1 && data == null) {
            // 如果是编辑模式 编辑数据又为空不跳转
        } else {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("seller_type", seller_type + "");
            bundle.putSerializable("data", data);
            bundle.putInt("type", type);
            intent.putExtras(bundle);
            intent.setClass(mContext, ShopInfoT2Activity.class);
            startActivityForResult(intent, 200);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        }
    }
}
